package yxy.game.pm2.command.union.leader;

import yxy.cherry.data.script.fomula;
import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.bean.Currency;
import yxy.game.pm2.bean.PlayerBean;
import yxy.game.pm2.bean.currency.CurrencyNotEnoughException;
import yxy.game.pm2.bean.currency.CurrencyType;
import yxy.game.pm2.bean.mq.CCReason;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.UnionLeaderMessage;
import yxy.game.pm2.message.UnionMessage;
import yxy.game.pm2.module.CurrencyHandle;
import yxy.game.pm2.module.battle.BattleHandle;
import yxy.game.pm2.module.playtype.PlayTypeHandle;
import yxy.game.pm2.module.playtype.PlayTypeModule;
import yxy.game.pm2.module.union.UnionBean;
import yxy.game.pm2.module.union.UnionHandle;
import yxy.game.pm2.module.union.UnionHandle.UnionPlayerEntry;
import yxy.game.pm2.module.union.UnionName;

/** 联盟盟主 联盟创建 */
@MessageType(proto = Command.UnionCreate, classtype = UnionLeaderMessage.UnionCreateRequest.class)
public class CMD_UnionCreate extends Command<UnionLeaderMessage.UnionCreateRequest> {

	@Override
	public void execute(MessageSource messageSource, UnionLeaderMessage.UnionCreateRequest request) {
		String playerCode = messageSource.getTag();
		String name = request.getName();
		String abbr = request.getAbbr();
		int flag = request.getFlag();

		UnionLeaderMessage.UnionCreateResponse.Builder response = UnionLeaderMessage.UnionCreateResponse.newBuilder();

		PlayerBean playerBean = PlayerBean.get(playerCode);
		if (playerBean == null) {
			send(messageSource, response, Result.DataError);
			return;
		}

		// 检查是否已经有联盟
		UnionPlayerEntry entry = new UnionPlayerEntry(playerCode);
		Result result = new UnionHandle().checkPlayerUnion(entry);
		if (!result.equals(Result.UnionOut)) {
			send(messageSource, response, result);
			return;
		}

		UnionName unionName = UnionName.getInstance();
		try {
			if (unionName.tryLock()) {
				// 检查名字
				// 字符整形 去掉空格 全角改半角
				name = UnionName.getInstance().checkName(name);
				abbr = UnionName.getInstance().checkName(abbr);
				result = new UnionHandle().checkName(name, abbr, null);
				if (!result.equals(Result.Success)) {
					send(messageSource, response, result);
					return;
				}

				// 扣费
				try {
					int costNumber = fomula.创建联盟花费元宝数.intValue();
					Currency cost = Currency.create(CurrencyType.CT_元宝, 0, costNumber);
					new CurrencyHandle().cost(playerCode, CCReason.联盟管理, cost);
				} catch (CurrencyNotEnoughException e) {
					send(messageSource, response, new Result(Result.CostFail, e.getType().getNumber(), e.getDataId()));// 消费不足
					return;
				}

				UnionBean union = UnionBean.create(playerBean, name, abbr, flag);

				UnionMessage.UnionMainResponse.Builder unionMessage = new UnionHandle().unionMain(union, playerCode);
				MessageEmitterHandler.getInstance().send(messageSource, unionMessage.build());

				PlayTypeHandle.saveUnion(playerCode, PlayTypeModule.get(playerCode));
				new BattleHandle().powerUpdate(playerCode);
				new UnionHandle().powerUpdate(union);
				new UnionHandle().levelUpdate(union);
			}
		} catch (Exception e) {
			e.printStackTrace();
			send(messageSource, response, Result.DataError);
			return;
		} finally {
			unionName.unlock();
		}

	}

	private void send(MessageSource messageSource, UnionLeaderMessage.UnionCreateResponse.Builder response, Result result) {
		response.setResult(result.packResult());
		MessageEmitterHandler.getInstance().send(messageSource, response.build());
	}
}